<?php
$form = $this->beginWidget('CActiveForm', array(
    'id' => 'union-form',
    'enableClientValidation' => true,
        ));
?>
<table class="form" style="width: 100%">   
    <tr>        
        <td class="label required"><?php echo $form->labelEx($model, 'name'); ?></td>
        <td class="input">
            <?php            
                echo $form->textField($model, 'name', array('style' => 'width:230px'));
                echo $form->error($model, 'name');
            ?>
        </td>
    </tr>
    <tr>        
        <td class="label"><?php echo $form->labelEx($model, 'address'); ?></td>
        <td class="input">
            <?php            
                echo $form->textField($model, 'address', array('style' => 'width:230px'));
            ?>
            <?php echo $form->error($model, 'address'); ?>
        </td>
    </tr>
    <tr>
        <td class="label required"><?php echo $form->labelEx($model, 'phone_number'); ?></td>
        <td class="input" >            
            <?php
            echo $form->textField($model, 'phone_number', array('style' => 'width:230px'));
            echo $form->error($model, 'phone_number');
            ?>
        </td>
    </tr>
    <tr>
        <td class="label required"><?php echo $form->labelEx($model, 'president'); ?></td>
        <td class="input" >            
            <?php
            echo $form->textField($model, 'president', array('style' => 'width:230px'));
            echo $form->error($model, 'president');
            ?>
        </td>
    </tr>
    <tr>
        <td class="label required"><?php echo $form->labelEx($model, 'allowance'); ?></td>
        <td class="input" >            
            <?php
            echo $form->textField($model, 'allowance', array('style' => 'width:230px'));
            echo $form->error($model, 'allowance');
            ?>
        </td>
    </tr>
    <tr>
        <td class="label required"><?php echo $form->labelEx($model, 'management_fee'); ?></td>
        <td class="input" >            
            <?php
            echo $form->textField($model, 'management_fee', array('style' => 'width:230px'));
            echo $form->error($model, 'management_fee');
            ?>
        </td>
    </tr>
    <tr>
			<td class="label"><?php echo $form->label($model, 'signing_contract_date'); ?></td>
			<td class="input">
            <?php            
                echo $form->textField($model, 'signing_contract_date', array('style' => 'width:230px', 'class' => 'datepicker', 'readonly'=>'true'));
                echo $form->error($model, 'signing_contract_date');
           ?>
        </td>
	</tr>
	<tr>
        <td class="label required"><?php echo $form->labelEx($model, 'dispatched_company'); ?></td>
        <td class="input" >            
            <?php
            echo $form->textField($model, 'dispatched_company', array('style' => 'width:230px'));
            echo $form->error($model, 'dispatched_company');
            ?>
        </td>
    </tr>
    <tr>
        <td colspan="2" class="cbutton">
<?php echo Html::buttonConfirm(t('BTN_SAVE'), 'statusConfirmed'); ?>
<?php echo Html::buttonConfirm(t('BTN_CANCEL'), 'statusCanceled', 'cancel'); ?>
        </td>
    </tr>
</table>    
<?php $this->endWidget(); ?>

<script type="text/javascript">
LGCMS.addTooltip();
$(document).ready(function() {
	popupCreateUnion = $('#popupCreateUnion');
	popupCreateUnion.find(".update-dialog-submit-button").click(function(){		
		
		// Set full submit name
	    submitName = '&createunion=true';
	    
	    // Get form from UpdateDialog
	    var form = popupCreateUnion.find( 'form' );
	    
	    // Get form data
	    var formData = form.serialize() + submitName;

	    //keep track value for combobox
	    var union_name, allowance, management_fee; 

	    popupCreateUnion.html( '<div class="ajax-loader" ></div>');
	    
	    // Make an AJAX call to submit form data
	    $.ajax({
	      'url': '<?php echo url('/union/create');?>',
	      'data': formData,
	      'type': 'post',
	      'dataType': 'json',
	      'success': function( data ){

	    	//keep data when save successful
	        if(data.status=='success')
	        {
	        	union_name = $(form).find('#Union_name').val();
	        	allowance = $(form).find('#Union_allowance').val();
	        	management_fee = $(form).find('#Union_management_fee').val();
	        }
	        
		    // Add returned contents to UpdateDialog
	        popupCreateUnion.html( data.content );

	        if(data.status=='success')
	        {
		        var option = '<option value="'+data.id+'" data="'+allowance+'||'+management_fee+'">'+union_name+'</option>';		        
		        $("#HiringCompany_union_id").append(option);
		        $("#HiringCompany_union_id").find('option[value="'+data.id+'"]').prop('selected', true);
		        $("#textfield_union_allowance").val(allowance);
		        $("#textfield_union_management_fee").val(management_fee);
		        popupCreateUnion.html("");
		        popupCreateUnion.dialog('close');
	        }	        	        
	      }
	    });
	});
	popupCreateUnion.find(".update-dialog-cancel-button").click(function(){
		popupCreateUnion.html("");
		popupCreateUnion.dialog('close');
    });
})

	$(function() {
	    $('#Union_signing_contract_date').datepicker({
	    	'dateFormat': 'dd-mm-yy',
	        changeMonth: true,
	        changeYear: true,
	        'buttonImage': '<?php echo themeUrl(); ?>/images/datepicker.gif',
            'showOn': 'both',
            'buttonImageOnly': 'true',
            'buttonText': "Chọn ngày",
            'showAnim': 'slideDown',
            'yearRange': '-60:+0',
	    });
	});
</script>
